package com.ndood.spider.taobao.web;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ndood.core.render.SeleniumDownloader;
import com.ndood.core.render.impl.PhantomJsDriverPool;
import com.ndood.spider.taobao.action.TaobaoAction;
import com.ndood.spider.taobao.bean.TaobaoGoods;
import com.ndood.spider.taobao.dao.TaobaoGoodsDao;
import com.ndood.spider.taobao.pipeline.TaobaoPipeline;
import com.ndood.spider.taobao.processor.TaobaoPageProcessor;

import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 爬虫控制器
 */
@RestController
@RequestMapping("/taobao")
public class TaobaoSpiderController {

	@Autowired
	private TaobaoPipeline taobaoPipeline;

	private AtomicBoolean isOk = new AtomicBoolean(false);
	
	@Autowired
	private TaobaoGoodsDao taobaoGoodsDao;
	
	//private static final String CONFIG_DIR = "C:/Users/Administrator/Desktop/lala's peach/";
	//private static final String CONFIG_DIR = "C:/Users/Administrator/Desktop/艾舒客旗舰店/";
	private static final String CONFIG_DIR = "C:/Users/Administrator/Desktop/罗拉的幸福/";
	
	@Autowired
	private TaobaoPageProcessor taobaoPageProcessor;
	
	/**
	 * 爬取一个静态文件上的连接
	 * @param cat
	 * @param catPath
	 * @param index
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/spider")
	public String spider() throws Exception {
		Spider spider = Spider.create(taobaoPageProcessor);
		spider.addUrl("https://taiwanguzaowei.taobao.com/search.htm?spm=2013.1.w5002-16316874251.1.7b2497faNURFae&search=y");
		spider.addPipeline(taobaoPipeline);
		spider.setDownloader(new SeleniumDownloader(5000, new PhantomJsDriverPool(), new TaobaoAction()));
		spider.thread(1);
		spider.setExitWhenComplete(true);
		spider.start();
		while (!isOk.get()) {
			Thread.sleep(1000);
		}
		isOk.set(false);
		return "ok";
	}
	
	@RequestMapping("/re_download_pic")
	public String reDownloadPic(String shopname) throws Exception {
		// Step1: 查询出所有商品
		List<TaobaoGoods> goods = taobaoGoodsDao.getListByShopNameOrderByCat(shopname);
		for (TaobaoGoods taobaoGoods : goods) {
			taobaoPipeline.redownload(taobaoGoods);
			System.out.println(taobaoGoods.getId());
			Thread.sleep(1000);
		}
		// Step3: 返回结果
		return "ok";
	}
	
	@RequestMapping("/spider2")
	public String spider2(String id) throws Exception {
		// Step1: 读取静态文件
		String fileStr = readFile(CONFIG_DIR + id +".txt");
		System.out.println(fileStr);
		Html html = new Html(fileStr);
		
		// Step2: 解析出商品详情信息
		TaobaoGoods goods = new TaobaoGoods();
		
		// 店铺名称
		String shopName = html.xpath("//*[@id='J_ShopInfo']/div[@class='tb-shop-info-wrap']/div[@class='tb-shop-info-hd']/div[@class='tb-shop-name']/dl/dd/strong/a/@title").get();
		goods.setShopName(shopName);

		// 获取商品ID
		goods.setId(id);
		
		// 获取商品名称
		String name = html.xpath("//*[@id='J_Title']/h3/text()").get();
		goods.setName(name);
		
		// 获取商品价格
		String price = html.xpath("//*[@id='J_PromoPriceNum']/text()").get();
		goods.setPrice(price);
		
		// 获取商品图片
		Map<String, String> imgUrls = new HashMap<String, String>();
		List<Selectable> imageNodes = html.xpath("//*[@id='J_UlThumb']/li/div/a/img").nodes();
		for (int i = 0; i < imageNodes.size(); i++) {
			Selectable imageNode = imageNodes.get(i);
			String goodsImgUrl = imageNode.xpath("//*/@src").get();
			goodsImgUrl = goodsImgUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "").replaceAll("_.webp", "");
			imgUrls.put(name + i, goodsImgUrl);
		}
		goods.setImgUrls(imgUrls);
		
		// 获取销量和评论数
		String commentsCount = html.xpath("//*[@id='J_RateCounter']/text()").get();
		String salesCount = html.xpath("//*[@id='J_SellCounter']/text()").get();
		goods.setCommentsCount(commentsCount);
		goods.setSalesCount(salesCount);
		
		// 获取商品参数
		List<Selectable> goodsDescNodes = html.xpath("//*[@id='attributes']/ul/li").nodes();
		for (int i=0;i<goodsDescNodes.size();i++) {
			Selectable goodsDescNode = goodsDescNodes.get(i);
			String line = goodsDescNode.xpath("//*/text()").get();
			String key = "";
			String value = "";
			if(line.split(":").length< 2){
				key = line.split("：")[0];
				value = line.split("：")[1];
			}else{
				key = line.split(":")[0];
				value = line.split(":")[1];
			}
			if(key.contains("品牌")){
				goods.setBrand(value);
			}else{
				goods.getParams().put(key, value);
			}
		}
		// 获取商品描述
		List<Selectable> descImgs = html.xpath("//*[@id='J_DivItemDesc']/div/p/img").nodes();
		if(descImgs==null||descImgs.size()==0){
			descImgs = html.xpath("//*[@id='J_DivItemDesc']/p/img").nodes();
		}
		if(descImgs==null||descImgs.size()==0){
			descImgs = html.xpath("//*[@id='J_DivItemDesc']/div/div/img").nodes();
		}
		if(descImgs==null||descImgs.size()==0){
			descImgs = html.xpath("//*[@id='J_DivItemDesc']/div/img").nodes();
		}
		if(descImgs==null||descImgs.size()==0){
			descImgs = html.xpath("//*[@id='J_DivItemDesc']/div/p/span/img").nodes();
		}
		Map<String, String> descUrls = new HashMap<String, String>();
		for (int i = 0; i < descImgs.size(); i++) {
			Selectable descImg = descImgs.get(i);
			String descUrl = descImg.xpath("//*/@src").get();
			String lazyUrl =  descImg.xpath("//*/@data-ks-lazyload").get();
			if(!StringUtils.isBlank(lazyUrl)){
				descUrl = lazyUrl.replaceAll("https:", "");
			}
			descUrl = descUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "").replaceAll("https:", "");
			descUrls.put("商品描述" + i, descUrl);
		}
		goods.setDescUrls(descUrls);
		taobaoPipeline.dealWithOneTaobaoGoods(goods);

		// Step3: 更新数据库
		while (!isOk.get()) {
			Thread.sleep(1000);
		}
		isOk.set(false);
		return "ok";
	}
	
	@RequestMapping("/modify")
	public String modify() throws Exception {
		// Step1: 查询出所有name为空的商品
		String targetUrls = "";
		List<TaobaoGoods> goods = taobaoGoodsDao.getList();
		for (TaobaoGoods TaobaoGoods : goods) {
			if(TaobaoGoods.getName()==null||TaobaoGoods.getPrice()==null){
				targetUrls = targetUrls + TaobaoGoods.getUrl() + ",";
			}
		}
		targetUrls = targetUrls.substring(0, targetUrls.lastIndexOf(","));

		// Step2: 开启爬虫，依次爬取商品
		Spider spider = Spider.create(taobaoPageProcessor);
		spider.addUrl(targetUrls.toString().split(","));
		spider.addPipeline(taobaoPipeline);
		spider.setDownloader(new SeleniumDownloader(5000, new PhantomJsDriverPool(), new TaobaoAction()));
		spider.thread(1);
		spider.setExitWhenComplete(true);
		spider.start();
		while (!isOk.get()) {
			Thread.sleep(1000);
		}
		isOk.set(false);
		return "ok";
	}

	/**
	 * 读取静态html文件内容
	 * @param fileName
	 * //*[@id="J_DivItemDesc"]/table/tbody/tr[2]/td[1]/a/img
	 * @return
	 */
	private String readFile(String fileName) {
		File myFile = new File(fileName);
		if (!myFile.exists()) {
			System.err.println("Can't Find " + fileName);
		}
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
			StringBuilder sb = new StringBuilder();
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
			in.close();
			return sb.toString();
		} catch (IOException e) {
			e.getStackTrace();
		}
		return null;
	}

	/**
	 * 关闭爬虫
	 * @return
	 */
	@RequestMapping("/stop")
	public String stop() {
		isOk.set(true);
		return "stoped";
	}
}
